// pages/order-comment/order-comment.js
var api = require('../../../config/api.js');
var util = require('../../../utils/util.js');
var app = getApp();
var p = 1;
var size = 20;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        book_list: [],
        imgUrl:api.RootUrl,
        commentList:[],
        comment:{}
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var page = this;
        page.setData({
            order_id: options.id,
        });
        wx.showLoading({
            title: "正在加载",
            mask: true,
        });
        util.request(api.OrderComment, {
          orderId: options.id},).then(function(res){
            wx.hideLoading();
            if (res.errno == 1) {
              wx.showModal({
                title: "提示",
                content: res.errmsg,
                showCancel: false,
                success: function (e) {
                  if (e.confirm) {
                    wx.navigateBack();
                  }
                }
              });
            }
            if (res.errno == 0) {
              for (var i in res.data.order.apiOrderDetailList) {
                //res.data.order.apiOrderDetailList[i].score = 3;
               // res.data.order.apiOrderDetailList[i].content = "";
                //res.data.order.apiOrderDetailList[i].apiBook.listPicUrl = [];
                page.data.comment.bookId = res.data.order.apiOrderDetailList[i].apiBook.id;
                page.data.comment.orderId = page.data.order_id;
                page.data.commentList.push(page.data.comment);
              }
              page.setData({
                book_list: res.data.order.apiOrderDetailList,
              });
            }
          });

    },

    setScore: function (e) {
        var page = this;
        var index = e.currentTarget.dataset.index;
        var score = e.currentTarget.dataset.score;
        var commentList = page.data.commentList;
        var book_list = page.data.book_list;
        book_list[index].score = score;
        page.setData({
          book_list: book_list
        });
        commentList[index].star = score;
    },
    contentInput: function (e) {
        var page = this;
        var index = e.currentTarget.dataset.index;
        page.data.commentList[index].content = e.detail.value;
    },
   


    commentSubmit: function (e) {
        var page = this;
        wx.showLoading({
            title: "正在提交",
            mask: true,
        });
        var commentList = page.data.commentList;
          util.request(api.CommentPost,{
                    commentList:commentList,
                },'post').then(function(res){
                  wx.hideLoading();
                  if (res.errno == 0) {
                    wx.showModal({
                      title: "提示",
                      content: res.errmsg,
                      showCancel: false,
                      success: function (e) {
                        if (e.confirm) {
                          wx.redirectTo({
                            url: "/pages/ucenter/order/order?status=4",
                          });
                        }
                      }
                    });
                  }
                  if (res.errno == 1) {
                    wx.showToast({
                      title: res.errmsg,
                      image: "/images/icon-warning.png",
                    });
                  }
                });
                
        

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
});